package com.re.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.re.api.UserApi;
import com.re.dao.UserEntity;
import com.re.dto.User;
import com.re.mapper.UserMapper;
import com.re.dao.UserDao;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

/**
 * 用户业务实现
 * @author re
 */
@Component
public class UserApiImpl implements UserApi {
    
    @Inject
    private UserDao userDao;
    
    @Inject
    private UserMapper userMapper;
    
    @Override
    public boolean save(User user) {
        UserEntity entity = userMapper.toEntity(user);
        return userDao.insert(entity) > 0;
    }
    
    @Override
    public User getById(Long id) {
        UserEntity entity = userDao.selectById(id);
        return userMapper.toDto(entity);
    }
    
    @Override
    public List<User> list() {
        List<UserEntity> entities = userDao.selectList(null);
        return userMapper.toDtoList(entities);
    }
    
    @Override
    public User getByUsername(String username) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        UserEntity entity = userDao.selectOne(queryWrapper);
        return userMapper.toDto(entity);
    }
    
    @Override
    public boolean update(User user) {
        UserEntity entity = userMapper.toEntity(user);
        return userDao.updateById(entity) > 0;
    }
    
    @Override
    public boolean removeById(Long id) {
        return userDao.deleteById(id) > 0;
    }
} 